package com.fortune.controller.acl;

import com.fortune.common.acl.JsonData;
import com.fortune.common.acl.PageQuery;
import com.fortune.dto.acl.MAclParam;
import com.fortune.entity.MRole;
import com.fortune.service.acl.MAclService;
import com.fortune.service.acl.MRoleService;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

//@Controller
@RequestMapping("/m/acl")
@Slf4j
public class MAclController {

    @Resource
    private MAclService mAclService;
    @Resource
    private MRoleService mRoleService;

    @RequestMapping("/save")
    @ResponseBody
    public JsonData saveAclModule(MAclParam param) {
        mAclService.save(param);
        return JsonData.success();
    }

    @RequestMapping("/update")
    @ResponseBody
    public JsonData updateAclModule(MAclParam param) {
        mAclService.update(param);
        return JsonData.success();
    }

    @RequestMapping("/page")
    @ResponseBody
    public JsonData list(@RequestParam("aclModuleId") Integer aclModuleId, PageQuery pageQuery) {
        return JsonData.success(mAclService.getPageByAclModuleId(aclModuleId, pageQuery));
    }

    @RequestMapping("acls")
    @ResponseBody
    public JsonData acls(@RequestParam("aclId") int aclId) {
        Map<String, Object> map = Maps.newHashMap();
        List<MRole> roleList = mRoleService.getRoleListByAclId(aclId);
        map.put("roles", roleList);
        map.put("users", mRoleService.getUserListByRoleList(roleList));
        return JsonData.success(map);
    }
}
